<?php


namespace app\model;

use think\facade\Log;
use think\Model;

/**
 * 通行记录
 */
class MqttCarPushInfo extends Model {

    public function save_push($recive) {
        $params=$recive['AlarmInfoPlate']['result']['PlateResult'];
        $ape_id=$recive['AlarmInfoPlate']['serialno'];
        $device=ViidDevice::where('ape_id',$ape_id)->find();
        if(!$device){
            Log::error("没有设备：".$ape_id);
            return false;
        }

        $path=config('capture.capture_path').'/'.$device['device_id'].'/'.date('Ym').'/'.date('d').'/';
        $data['license']=$params['license'];
        $data['device_id']=$device['device_id'];
        $data['photo_url']=$path;
        $data['bright']=$params['bright'];
        $data['carBright']=$params['carBright'];
        $data['carColor']=$params['carColor'];
        // $data['clean_time']=$params['clean_time'];
        $data['colorType']=$params['colorType'];
        $data['colorValue']=$params['colorValue'];
        $data['confidence']=$params['confidence'];
        $data['direction']=$params['direction'];
        $data['license_ext_type']=$params['license_ext_type'];
        $this->save($data);
        save_base_img($this->id.'_small',$params['imageFragmentFile'],$path);
        save_base_img($this->id.'_bg',$params['imageFile'],$path);
        ViidDevice::where('device_id',$device['device_id'])->save(['last_time'=>date('Y-m-d H:i:s')]);
    }

    public function save_car($params) {
        $content=json_decode($params,true);
        $all_params=$content['MotorVehicleListObject']['MotorVehicleObject'];
        foreach ($all_params as $params){
            $path=config('capture.capture_path').'/'.$params['DeviceID'].'/'.date('Ym').'/'.date('d').'/';
            $data['license']=$params['MotorVehicleID'];
            $data['device_id']=$params['DeviceID'];
            $data['photo_url']=$params['MotorVehicleID'];
            $this->save($data);
            $img=file_get_contents($params['StorageUrl1']);
            $fp=fopen($path.$this->id.'.jpg');
            fwrite($fp,$img);
            fclose($fp);
            ViidDevice::where('device_id',$params['DeviceID'])->save(['last_time'=>date('Y-m-d H:i:s')]);
        }
        //save_base_img($this->id.'_small',$single['small_res'],$path);
    }
}